using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using Entity;
using Utilities;


namespace DAL
{
    public class InLogDataAccess : FreshCmsAccessBase
    {
        #region code CMS
        //public List<InLog> GetInLogList()
        //{
        //    try
        //    {
        //        const string spName = "spi_InLog_GetList";

        //        var InLogList = DbHelper.ExecuteSPList<InLog>(spName);
        //        return InLogList;
        //    }
        //    catch (Exception exp)
        //    {
        //        Logger.WriteLog(Logger.LogType.Error,
                                //"File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                //" --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                //" --- Mess: " + exp.ToString());

        //        return new List<InLog>();
        //    }
        //}

        public List<InLog> GetInLogListByPaging(string mail, int action, DateTime from, DateTime to, int pageIndex, int pageSize, ref int totalRecord)
        {
            try
            {
                const string spName = "spi_InLog_GetByPaging";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@UserMail", mail),
                                        new SqlParameter("@LogAction", action),
                                        new SqlParameter("@From", from),
                                        new SqlParameter("@To", to),
                                        new SqlParameter("@PageIndex", pageIndex),
                                        new SqlParameter("@PageSize", pageSize),
                                        new SqlParameter("@TotalRecord", totalRecord)
                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
                var InLogList = DbHelper.ExecuteSqlList<InLog>(sqlCommand);
                if (sqlCommand.Parameters["@TotalRecord"].Value != null)
                {
                    totalRecord = (int)sqlCommand.Parameters["@TotalRecord"].Value;
                }

                return InLogList;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return new List<InLog>();
            }
        }

        //public InLog GetInLogById(int logId)
        //{
        //    try
        //    {
        //        const string spName = "spi_InLog_GetById";
        //        var sqlParams = new[]
        //                            {
        //                                new SqlParameter("@LogId", logId)
        //                            };

        //        var InLog = DbHelper.ExecuteSPList<InLog>(spName, sqlParams).SingleOrDefault();
        //        return InLog;
        //    }
        //    catch (Exception exp)
        //    {
        //        Logger.WriteLog(Logger.LogType.Error, exp.ToString());

        //        return null;
        //    }
        //}

        public int InsertInLog(InLog InLog)
        {
            try
            {
                const string spName = "spi_InLog_Insert";
                var sqlParams = new[]
                                    {

										new SqlParameter("@UserId", InLog.UserId),
										new SqlParameter("@LogAction", InLog.LogAction),
										new SqlParameter("@Field1", InLog.Field1),
										new SqlParameter("@Field2", InLog.Field2),
										new SqlParameter("@Field3", InLog.Field3),
										new SqlParameter("@LogId", InLog.LogId),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@LogId"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@LogId"].Value != null)
                {
                    return (int) sqlCommand.Parameters["@LogId"].Value;
                }

				return 0;


            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        //public int UpdateInLog(InLog InLog)
        //{
        //    try
        //    {
        //        const string spName = "spi_InLog_Update";
        //        var sqlParams = new[]
        //                            {
        //                                new SqlParameter("@LogId", InLog.LogId)
        //                                , new SqlParameter("@UserId", InLog.UserId)
        //                                , new SqlParameter("@LogAction", InLog.LogAction)
        //                                , new SqlParameter("@Field1", InLog.Field1)
        //                                , new SqlParameter("@Field2", InLog.Field2)
        //                                , new SqlParameter("@Field3", InLog.Field3)
        //                                , new SqlParameter("@LogDate", InLog.LogDate)
        //                            };

        //        var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

        //        return returnVal;
        //    }
        //    catch (Exception exp)
        //    {
        //        Logger.WriteLog(Logger.LogType.Error, exp.ToString());

        //        return -1;
        //    }
        //}

        public int DeleteInLog(string lstLogChecked)
        {
            try
            {
                const string spName = "spi_InLog_Delete";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@LstLogId", lstLogChecked)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        public int DeleteAllInLog()
        {
            try
            {
                const string spName = "spi_InLog_DeleteAll";

                var returnVal = DbHelper.ExecuteNonQuery(spName);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

		#endregion
    }
}
